package com.qf.service.impl;

import com.qf.entity.Goods;
import com.qf.listener.GoodsItemQueueListener;
import com.qf.service.IItemService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ItemServiceImpl implements IItemService {

    @Autowired
    private Configuration configuration;

    @Override
    public void createHtml(Goods goods) throws  Exception{

        // 1.准备数据
        Map<String, Object> map = new HashMap<>();
        map.put("gname", goods.getGname());
        map.put("gprice", goods.getGprice());
        map.put("gid", goods.getId());
        map.put("gpicList", goods.getGpic().split("\\|"));

        // 2.准备模板
        Template template = configuration.getTemplate("goodsItem.ftl");

        // 获取页面输出的目录里
        String staticPath = GoodsItemQueueListener.class.getClassLoader().getResource("static").getPath();

        // 3.准备视图输出的位置
        FileWriter writer = null;
        // 4.生成页面
        try {
            writer = new FileWriter(staticPath + File.separator + goods.getId() + ".html");
            template.process(map, writer);
        } catch (IOException e) {
            log.error("生成静态页面出错", e);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
}
